#include "MobileCfmUserInfoDlg.h"

#include <core/ImageMgr.h>
#include <core/StyleMgr.h>
#include <uam/UserAccountManager.h>
#include <widgets/SCheckComboBox.h>
#include <widgets/SLineEdit.h>

#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QStylePainter>
#include <QTimer>
#include <QTimerEvent>
#include <QVBoxLayout>
#include <QWidget>
#include <regex>
#include <set>

MobileCfmUserInfoDlg::MobileCfmUserInfoDlg(QWidget* p /*= nullptr*/)
    : QWidget(p) {
  QVBoxLayout* vLayout = new QVBoxLayout(this);
  vLayout->setContentsMargins(0, 0, 0, 0);

  QVBoxLayout* vLayoutFirst = new QVBoxLayout;
  vLayoutFirst->setContentsMargins(178, 0, 0, 0);
  QLabel* checkSafeLabel = new QLabel(this);
  SImage* checkSafeImg = ImgMgr::instance().image(
      "auth_icon_check_safe", WidgetStatus::kNormal, devicePixelRatioF());
  if (checkSafeImg)
    checkSafeLabel->setPixmap(QPixmap::fromImage(*checkSafeImg));
  checkSafeLabel->setFixedSize(45, 53);
  vLayoutFirst->addWidget(checkSafeLabel);
  QSpacerItem* fItem = new QSpacerItem(0, 3);
  vLayoutFirst->addSpacerItem(fItem);
  vLayout->addLayout(vLayoutFirst);

  QVBoxLayout* vLayoutSecond = new QVBoxLayout;
  vLayoutSecond->setContentsMargins(142, 0, 0, 0);
  QLabel* strLabel = new QLabel(tr("您已成功完成手机验证"), this);
  strLabel->setStyleSheet("QLabel{color:rgb(126, 211, 33);}");
  vLayoutSecond->addWidget(strLabel);
  vLayout->addLayout(vLayoutSecond);
  vLayout->addSpacing(13);

  QHBoxLayout* hLThird = new QHBoxLayout;
  hLThird->setContentsMargins(47, 0, 0, 0);
  // strLabel = new
  // QLabel(tr("以下是您注册qeubee时提供的信息，由于信息完整性会影\n响您可得到的数据功能权限，所以麻烦确认您的最新信息哦~"),
  // this);
  strLabel =
      new QLabel(tr("以下是您注册qeubee时提供的信息，由于信息完整性会影响您可得"
                    "到的数据功能权限，所以麻烦确认您的最新信息哦~"),
                 this);
  hLThird->addWidget(strLabel);
  vLayout->addLayout(hLThird);
  vLayout->addSpacing(20);

  const CUserAccountInfo& usrInfo =
      SSUserAccountManager::instance().GetUserInfo();

  QGridLayout* formLayout = new QGridLayout;
  formLayout->setContentsMargins(59, 0, 0, 7);
  formLayout->setSpacing(53);
  formLayout->setVerticalSpacing(18);
  QLabel* institution = new QLabel(tr("机构:"), this);
  institution->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_institution =
      new SLineEdit(QString::fromLocal8Bit(usrInfo.m_CompanyName), this);
  m_institution->setFixedSize(188, 22);
  formLayout->addWidget(institution, 0, 0);
  formLayout->addWidget(m_institution, 0, 1);

  QLabel* department = new QLabel(tr("部门:"), this);
  department->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_department =
      new SLineEdit(QString::fromLocal8Bit(usrInfo.m_Department), this);
  m_department->setFixedSize(188, 22);
  formLayout->addWidget(department, 1, 0);
  formLayout->addWidget(m_department, 1, 1);

  QLabel* position = new QLabel(tr("职位:"), this);
  position->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_position = new SLineEdit(QString::fromLocal8Bit(usrInfo.m_Title), this);
  m_position->setFixedSize(188, 22);
  formLayout->addWidget(position, 2, 0);
  formLayout->addWidget(m_position, 2, 1);

  QLabel* tel = new QLabel(tr("固话:"), this);
  tel->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_telephone =
      new SLineEdit(QString::fromLocal8Bit(usrInfo.m_Telephone), this);
  m_telephone->setFixedSize(188, 22);
  formLayout->addWidget(tel, 3, 0);
  formLayout->addWidget(m_telephone, 3, 1);

  QLabel* qq = new QLabel(tr("QQ:"), this);
  qq->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_qq = new SLineEdit(QString::fromLocal8Bit(usrInfo.m_QQ), this);
  m_qq->setFixedSize(188, 22);
  formLayout->addWidget(qq, 4, 0);
  formLayout->addWidget(m_qq, 4, 1);

  QLabel* type = new QLabel(tr("业务类型:"), this);
  type->setStyleSheet(
      QString("color:#%1")
          .arg(StyleMgr::instance().color("SupportingWord").rgb(), 0, 16));
  m_typeCheckBox = new SCheckComboBox(this, false, "");
  for (int i = 0; i < 8; i++) m_typeCheckBox->addItem(loadTreeData(i));
  QString strTempType = QString("%1").arg(usrInfo.m_BussinessType);
  if (strTempType.isEmpty()) {
    m_typeCheckBox->setCurrentText(tr("请选择您的业务类型"));
  } else {
    QStringList strList = strTempType.split("|");
    std::set<QString> m_setType;
    for (int nL = 0; nL < strList.size(); nL++) {
      m_setType.insert(strList[nL]);
    }
    QString str = "";
    for (auto t : m_setType) {
      str += t;
      str += "、";
    }
    m_typeCheckBox->setCurrentText(str);
  }

  m_typeCheckBox->setFixedSize(188, 22);
  formLayout->addWidget(type, 5, 0);
  formLayout->addWidget(m_typeCheckBox, 5, 1);

  vLayout->addLayout(formLayout);

  m_submit = new QPushButton(tr("提交"), this);
  m_submit->setFixedSize(94, 22);

  m_submit->setStyleSheet(
      QString("QPushButton{background:#%1; "
              "border-radius:0px;color:#FFE5C7}\nQPushButton:hover{background:#"
              "%2}\n"
              "QPushButton:disabled{background-color:#%3;color:#%4;}})")
          .arg(StyleMgr::instance().color("ButtonNormal").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("Hover").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("ButtonDisable").rgb(), 0, 16)
          .arg(StyleMgr::instance().color("TxtDisabled").rgb(), 0, 16));

  QHBoxLayout* hSubmitLayout = new QHBoxLayout;
  hSubmitLayout->setContentsMargins(45, 20, 0, 0);
  hSubmitLayout->addWidget(m_submit);
  vLayout->addLayout(hSubmitLayout);
}

void MobileCfmUserInfoDlg::paintEvent(QPaintEvent* event) {
  QWidget::paintEvent(event);
  QStylePainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);
  // painter.setPen(StyleMgr::instance().color("Up_Increase_H1"));
  painter.setPen(QColor(213, 34, 34));
  QRect starR;
  QString strStar = "*";
  QFontMetrics fm(font());

  int startX = rect().x() + 50;
  int starWidth = fm.horizontalAdvance(strStar);
  for (int i = 0; i < 3; i++) {
    starR.setRect(startX, rect().y() + 141 + i * 33, starWidth,
                  rect().height());
    painter.drawText(starR, strStar, QTextOption(Qt::AlignLeft));
  }
}

QString MobileCfmUserInfoDlg::loadTreeData(int index) {
  switch (index) {
    case 0: {
      return tr("交易");
      break;
    }
    case 1: {
      return tr("投资");
      break;
    }
    case 2: {
      return tr("主管领导");
      break;
    }
    case 3: {
      return tr("销售");
      break;
    }
    case 4: {
      return tr("研究");
      break;
    }
    case 5: {
      return tr("中后台");
      break;
    }
    case 6: {
      return tr("IT");
      break;
    }
    case 7: {
      return tr("其他");
      break;
    }
    case 8: {
      return tr("反选");
      break;
    }
    default:
      break;
  }
  return "";
}
